<?php

namespace Wap\Controller;

use Home\Model\WechatUserModel;
use Think\Controller;
use Think\Model;
use User\Model\RecommendModel;
use Wap\Service\WeixinService;

class RecommendController extends WapController
{
    private $s_appid = "wx9fee8af7befe3df5";
    private $s_appsecret = "1282128572e92b0e91d190b3bf882f02";

    public function index()
    {
        $rec_id = I("get.recid", "", "");
        if (empty($rec_id)) {
            return;
        }
        $union_id = I("get.unionid", "", "");
        if (empty($union_id)) {
            return;
        }
        $url = C('SITE_URL') . "/Wap/Recommend/entry?rec_id=" . $rec_id . "&union_id=" . $union_id;
        $url = WeixinService::snsapiUserInfoLink($url, $this->s_appid);
        header("Location: " . $url);
        exit();
    }

    public function entry()
    {
        $code = I("get.code", "", "");
        if (empty($code)) {
            return;
        }
        //--获取微信用户信息--
        $user = WeixinService::setUser($this->s_appid, $this->s_appsecret, $code);
        if (empty($user['openid'])) {
            return;
        }
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $user['access_token'] . "&openid=" . $user['openid'] . "&lang=zh_CN";
        $userinfo = self::simpleGet($url);;
        $userinfo = json_decode($userinfo, true);
        if (!empty($userinfo['errcode'])) {
            return;
        }
        $cond['rec_id'] = I("get.rec_id", "", "");
        $recommond = M("recommend")->where($cond)->find();
        if (empty($recommond)) {
            return;
        }
        $url = $recommond['url'];
        $cond_user['ori_id'] = $recommond['ori_id'];
        $cond_user['union_id'] = $userinfo['unionid'];
        $wechat_user = M("wechat_user")->where($cond_user)->find();
        if (!empty($wechat_user)) {
            header("Location: " . $url);
            exit();
        }
        $data["ori_id"] = $recommond['ori_id'];
        $data['union_id'] = $userinfo['unionid'];
        $data['status'] = RecommendModel::STATUS_PENDING;
        $data['rec_union_id'] = I("get.union_id", "", "");
        M("wechat_user")->add($data);//---添加推荐用户与用户的关系--
        header("Location: " . $url);
        exit();
    }

    private static function simpleGet($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 500);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }

    public function code()
    {
        $url = I("get.url", "", "");
        $rec_id = I("get.rec_id", "", "");
        $union_id = I("get.union_id", "", "");
        Vendor("phpqrcode.phpqrcode");
        $QRcode = new \QRcode();
        $url = C("SITE_URL") . "/Wap/Recommend/index?recid=" . $rec_id . "&unionid=" . $union_id;
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        $QRcode::png($url, false, $level, $size);
    }

    public function view()
    {
        $rec_id = I("get.id", "", "");
        if (empty($rec_id)) {
            return;
        }
        $code = I("get.code", "", "");
        if (empty($code)) {
            $url = C('SITE_URL') . "/Wap/Recommend/view?id=" . $rec_id;
            $url = WeixinService::snsapiUserInfoLink($url, $this->s_appid);
            header("Location: " . $url);
            exit();
        }
        $user = WeixinService::setUser($this->s_appid, $this->s_appsecret, $code);
        if (empty($user['openid'])) {
            return;
        }
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $user['access_token'] . "&openid=" . $user['openid'] . "&lang=zh_CN";
        $userinfo = self::simpleGet($url);;
        $userinfo = json_decode($userinfo, true);
        if (!empty($userinfo['errcode'])) {
            return;
        }
        $cond['rec_id'] = $rec_id;
        $cond['union_id'] = $userinfo['unionid'];
        $user_recommend = M("wuser_recommend")->where($cond)->find();
        $first_rec = empty($user_recommend) ? 0 : $user_recommend['first_rec'];
        $this->assign("rec_count", $first_rec);

        $cond_rec['rec_id'] = $rec_id;
        $recommend = M("recommend")->where($cond_rec)->find();
        $recommend_name = $recommend['name'] ? $recommend['name'] : "";
        $this->assign("recommend_name", $recommend_name);
        $this->assign("recid", $rec_id);
        $this->assign("unionid", $userinfo['unionid']);
        $this->assign("picurl", $recommend['picurl']);
        $this->display();
    }

    public function get_img()
    {
        $recid = I("get.recid", "", "");
        $unionid = I("get.unionid", "", "");
        $picurl = I("get.picurl", "", "");
        WechatUserModel::get_img_show($recid, $unionid, $picurl, "");
    }
}